package com.im323.ip.impl;

import com.im323.common.util.HttpClientUtils;
import com.im323.common.util.IpModel;
import com.im323.common.util.IpUtil;
import com.im323.ip.IpConverter;

public class IpCNConvert extends IpConverter {

	@Override
	public IpModel parse() {
		long sta = System.currentTimeMillis();
		String doGet = HttpClientUtils.doGet(IpUtil.URL_IPCN);

		String startFlag = "<code>";
		String endFlag = "</code>";

		IpModel ipModel = null;
		if (doGet != null && doGet.contains(startFlag)) {
			int startIndex = doGet.indexOf(startFlag) + startFlag.length();
			int endIndex = doGet.indexOf(endFlag, startIndex);

			String ip = doGet.substring(startIndex, endIndex);

			startIndex = doGet.indexOf(startFlag, endIndex);
			endIndex = doGet.indexOf(endFlag, startIndex);
			String locationIsp = doGet.substring(startIndex, endIndex);

			String location = locationIsp.split(" ")[0];
			String isp = locationIsp.split(" ")[1];

			ipModel = new IpModel();
			ipModel.setIpAddress(ip);
			ipModel.setLocation(location);
			ipModel.setIsp(isp);
			ipModel.setDelay(System.currentTimeMillis() - sta);
			ipModel.setUrl(IpUtil.URL_IPCN);
		}
		return ipModel;
	}

}
